package com.hatic.purchase;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.DateUtil;
import org.jxstar.util.key.CodeCreator;
import org.jxstar.util.key.KeyCreator;

/**
 * 单一来源方案申请事件处理
 * @author caojiaxin
 * @version 1.0,2011-11-03
 */
public class PurOneCHKBO extends BusinessObject{

	private static final long serialVersionUID = 1L;

	/**
	 * 单一来源方案申请审批通过后，产生一条采购单记录
	 * @param chkId
	 * @return
	 */
	public String onChkPass(String chkId){
		if(chkId == null || chkId.equals("")){
			_log.showDebug("没有获取到单一来源方案申请主键");
			return _returnFaild;
		}
		if(!createPurApply(chkId)){
			_log.showDebug("创建单一来源采购单失败");
			return _returnFaild;
		}
		return _returnSuccess;
	}

	/**
	 * 创建采购单
	 * @param chkId
	 * @return
	 */
	private boolean createPurApply(String chkId) {
		String querySql="select project_id,project_code,project_name,money_type,project_user,auth_type,"+
		" project_userid,check_fileno,check_name,project_money,dept_id,dept_name,done_deptid,done_deptname, "+
		" apply_userid,apply_user,apply_date,project_content,provider_names,add_userid from pur_one_chk where one_chkid = ?";
		
		_log.showDebug("==============one_chkid='"+chkId+"';sql="+querySql);
		
		DaoParam qparam = _dao.createParam(querySql);
		qparam.addStringValue(chkId);
		Map<String, String> qmap=_dao.queryMap(qparam);
		
		String applyId=KeyCreator.getInstance().createKey("pur_apply");
		String applyCode=CodeCreator.getInstance().createCode("pur_apply");
		
		String createSql="insert into pur_apply(apply_id,apply_code,project_id,project_code, "+
		" project_name,money_type,project_user,project_userid,check_fileno,check_name,project_money,dept_id,dept_name,done_deptname,"+
		" done_deptid,pur_type,apply_status,apply_userid,apply_user,apply_date,project_content,provider_name,auditing,add_userid,add_date,check_id,apply_name,auth_type )"+
		" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
		
		_log.showDebug("==============sql="+createSql);
		
		DaoParam cparam = _dao.createParam(createSql);
		
		cparam.addStringValue(applyId);
		cparam.addStringValue(applyCode);
		cparam.addStringValue(qmap.get("project_id"));
		cparam.addStringValue(qmap.get("project_code"));
		cparam.addStringValue(qmap.get("project_name"));
		cparam.addStringValue(qmap.get("money_type"));
		cparam.addStringValue(qmap.get("project_user"));
		cparam.addStringValue(qmap.get("project_userid"));
		cparam.addStringValue(qmap.get("check_fileno"));
		cparam.addStringValue(qmap.get("check_name"));
		cparam.addDoubleValue(qmap.get("project_money"));
		cparam.addStringValue(qmap.get("dept_id"));
		cparam.addStringValue(qmap.get("dept_name"));
		cparam.addStringValue(qmap.get("done_deptname"));
		cparam.addStringValue(qmap.get("done_deptid"));
		cparam.addStringValue("dy");
		cparam.addStringValue("8");
		cparam.addStringValue(qmap.get("apply_userid"));
		cparam.addStringValue(qmap.get("apply_user"));
		cparam.addDateValue(qmap.get("apply_date"));
		cparam.addStringValue(qmap.get("project_content"));
		cparam.addStringValue(qmap.get("provider_names"));
		cparam.addStringValue("0");
		cparam.addStringValue(qmap.get("add_userid"));
		cparam.addDateValue(DateUtil.getToday());
		cparam.addStringValue(chkId);
		cparam.addStringValue(qmap.get("project_name"));
		cparam.addStringValue(qmap.get("auth_type"));
		
		return _dao.update(cparam);
	}
	
	
	
}
